﻿using BySoft.Server.Business;
using BySoft.Server.Common;
using BySoft.Server.Common.Entity;
using BySoft.Server.Common.Enum;
using BySoft.Server.Consts;
using BySoft.Web.Utility;
using OneSoft.EntityCore;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BySoft.Web.Site.MasteData
{
    public partial class CustomerView : AuthBasePage
    {
        
        private string m_strsequenceName
        {
            get
            {
                return "MD_Customer";
            }
        }
        private string m_strTbl = "MD_Customer";

        private DataTable CreateDT()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("CustomerDetailID");
            dt.Columns.Add("CustomerID");
            dt.Columns.Add("ContactName");
            dt.Columns.Add("Tel");
            dt.Columns.Add("Job");
            dt.Columns.Add("Mobile");
            dt.Columns.Add("Email");
            dt.Columns.Add("QQ");
            return dt;
        }
        /// <summary>
        /// 联系人明细
        /// </summary>
        public DataTable CustomerDetailDT
        {
            get
            {
                if (Session["CustomerDetailDT"] != null)
                {
                    return (DataTable)Session["CustomerDetailDT"];
                }
                else
                {
                    return CreateDT();
                }
            }
            set
            {
                Session["CustomerDetailDT"] = value;
            }
        }



        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindInFo();
            }
        }
        /// <summary>
        /// 绑定页面数据
        /// </summary>
        private void BindInFo()
        {
            string strCode = HttpUtility.UrlDecode(UIHelper.GetQueryStringValue("id").Trim());
            //获取客户
            GetCustomer(strCode);
            //获取供应商明细
            GetCustomerdetail(strCode);

        }
        #region 绑定资料
        
        /// <summary>
        /// 获取客户明细
        /// </summary>
        /// <param name="strCode">供应商ID</param>
        private void GetCustomerdetail(string strCode)
        {
            if (!string.IsNullOrEmpty(strCode))
            {
                DataSet detail = MdCustomerdetail.GetDSByCondition("CustomerID='" + strCode + "'");
                DataTable dt = CreateDT();
                CustomerDetailDT = CommonMethod.CopyDTtoDT(detail.Tables[0], dt);
                BindData();
            }
        }

        /// <summary>
        /// 获取客户
        /// </summary>
        /// <param name="strCode">供应商ID</param>
        private void GetCustomer(string strCode)
        {
            if (!string.IsNullOrEmpty(strCode))
            {
                MdCustomerEntity Customer = MdCustomer.GetMdCustomerByID(strCode);
                if (Customer != null)
                {
                    this.lblCustomerID.Text = Customer.CustomerID;
                    this.lblCustomerName.Text = Customer.CustomerName;
                    this.lblCustomerScaler.Text = SysParameter.GetParameterName("CustomerScaler", Customer.CustomerScaler.Trim());
                    this.lblCustomerFrom.Text = SysParameter.GetParameterName("CustomerFrom", Customer.CustomerFrom.Trim());
                    this.lblCompanyAddr.Text = Customer.Addr;
                    this.lblMasterPerson.Text = Customer.MasterPerson;
                }
            }
        }
        #endregion


        protected void btnAdd_Click(object sender, EventArgs e)
        {
            DataTable dt = CustomerDetailDT.Copy();
            DataRow row = dt.NewRow();
            dt.Rows.Add(row);

            CustomerDetailDT = dt;
            BindData();
        }


        private void BindData()
        {
            if (CustomerDetailDT.Rows.Count > 0)
            {
                this.grdDetail.DataSource = CustomerDetailDT;
                grdDetail.DataBind();
                SetControlAttr(new MdCustomerdetailEntity(), grdDetail);
            }
            else
            {
                UIHelper.BindEmptyGridView(grdDetail, CreateDT());
            }
            UIHelper.RunScript(this.Page, "iframeAutoFit();");
        }
    }
}